package com.tyy.springboot;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@Slf4j
public class PPTXToImageConverter {

    public static void convertPPTXToImages(String inputFilePath, String outputFolderPath) throws IOException, TranscoderException {
        // 设置备用字体
        System.setProperty("awt.useSystemAAFontSettings", "on");
        System.setProperty("swing.aatext", "true");
        System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
        System.setProperty("sun.java2d.font.SunGraphics2D.fontRenderContextHintKey", "VALUE_TEXT_ANTIALIAS_ON");


        File inputFile = new File(inputFilePath);
        File outputFolder = new File(outputFolderPath);
        if (!outputFolder.exists()) {
            outputFolder.mkdirs();
        }
        // 调整最小膨胀比率
        ZipSecureFile.setMinInflateRatio(0.001);
        try (XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(inputFile))) {
            Dimension pgsize = ppt.getPageSize();
            int slideNumber = 1;
            for (XSLFSlide slide : ppt.getSlides()) {
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                graphics.setColor(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
                slide.draw(graphics);

                // Save as JPEG
                String outputFilePath = outputFolderPath + File.separator + "slide_" + slideNumber + ".jpg";
                try (FileOutputStream out = new FileOutputStream(outputFilePath)) {
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
                    param.setQuality(1f, false);
                    encoder.setJPEGEncodeParam(param);
                    encoder.encode(img);
                    log.info("Slide " + slideNumber + " saved as " + outputFilePath);

                }
                slideNumber++;
            }
        }
    }

    public static void main(String[] args) {
        try {
            convertPPTXToImages("D:\\tyy\\Downloads\\ppt转图片 多线程解析\\2.pptx", "output");
            // convertPPTXToImages("input.pptx", "output");
        } catch (IOException | TranscoderException e) {
            e.printStackTrace();
        }
    }
}